package com.renjibo.sharkerwave.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.kevin.wraprecyclerview.WrapAdapter;
import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.adapter.HomeAdapter;
import com.renjibo.sharkerwave.models.Course;
import com.renjibo.sharkerwave.models.FirstHand;
import com.renjibo.sharkerwave.models.data.AdBanner;
import com.renjibo.sharkerwave.models.data.CourseData;
import com.renjibo.sharkerwave.network.SharkerParams;
import com.renjibo.sharkerwave.ui.view.GlideImageLoader;
import com.renjibo.sharkerwave.utils.ShowHideTop;
import com.youth.banner.Banner;
import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 学习首页面
 */
public class StudyFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    List<String> images = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<com.renjibo.sharkerwave.models.Banner> bannerArray = new ArrayList<>();
    private Banner main_banner;
    //數據集合
    private List<Course> tryCourse = new ArrayList<>();
    private List<Course> qualityCourse = new ArrayList<>();

    private RecyclerView home_recyclerView;
    private View viewPagerView;

    private boolean one;
    private boolean two;
    private boolean three;
    private List<Course> topicCourse;
    private int HOME_COUNT = 3;

    public StudyFragment() {
    }

    public static StudyFragment newInstance(String param1, String param2) {
        StudyFragment fragment = new StudyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("StudyFragment", "开始请求");
        if (FirstHand.isHost()) {
            listTry("list_try", "", HOME_COUNT, 0);
            listCourse("list_course", "", HOME_COUNT, 0, 1);
            listTopic("list_topic", HOME_COUNT, 0);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        ShowHideTop.setWindow(getActivity(), true);
        initData();

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("FreeAuditionFragment", "onResume");
        ShowHideTop.setWindow(getActivity(), true);

    }

    private void initData() {
        images.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        images.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        images.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        images.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_study, container, false);
        addHeader(view);
        //请求头部
        listBanner();

        return view;
    }

    public void addHeader(View view) {
        home_recyclerView = (RecyclerView) view.findViewById(R.id.home_recyclerView);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        home_recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //头部
        viewPagerView = LayoutInflater.from(getActivity()).inflate(R.layout.banner_title, null);
        main_banner = (Banner) viewPagerView.findViewById(R.id.main_banner);
        if (FirstHand.isHost()) {
            listTry("list_try", "", HOME_COUNT, 0);
//            listCourse("list_course", "", 0, 1);
//            listTopic("list_topic", 0);
        }
    }

    /**
     * 头部轮播图 banner
     *
     * @param main_banner
     */
    public void setTopBanner(Banner main_banner) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(R.drawable.banner);
        list.add(R.drawable.banner);
        list.add(R.drawable.banner);
        list.add(R.drawable.banner);
        list.add(R.drawable.banner);
        //设置图片加载器
        main_banner.setImageLoader(new GlideImageLoader());
        //设置图片集合 真实数据
        main_banner.setImages(bannerArray);
//        //测试数据
//        Toast.makeText(getActivity(), "re:" + bannerArray.size(), Toast.LENGTH_SHORT).show();
//        main_banner.setImages(list);
        //设置轮播时间
        main_banner.setDelayTime(3000);
        //banner设置方法全部调用完毕时最后调用
        main_banner.start();
    }

    /**
     * 轮播图
     */
    void listBanner() {
        SharkerParams params = new SharkerParams("list_banner");
        x.http().post(params, new Callback.CommonCallback<AdBanner>() {
            @Override
            public void onSuccess(AdBanner result) {
                Log.d("StudyFragment", "轮播图result:" + result);
                List<com.renjibo.sharkerwave.models.Banner> bannerImage = result.banner;
                bannerArray.addAll(bannerImage);

//                Toast.makeText(getActivity(),"re: false"+bannerArray.size(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                setTopBanner(main_banner);
            }
        });
    }

    /**
     * 试听课程列表
     *
     * @param uri
     * @param category
     * @param page_index
     */
    void listTry(String uri, String category, int page_size, int page_index) {
        listCourse(uri, category, page_size, page_index, 0);
    }

    /**
     * 精品专辑列表
     *
     * @param uri
     * @param page_index
     */
    void listTopic(String uri, int page_size, int page_index) {
        listCourse(uri, "", page_size, page_index, 0);
    }

    /**
     * 精品课程列表
     *
     * @param uri
     * @param category
     * @param page_index
     * @param sort_by
     */
    public void listCourse(final String uri, final String category, int page_size, int page_index, int sort_by) {
        SharkerParams params = new SharkerParams(uri);

        if (!TextUtils.isEmpty(category)) {
            params.addBodyParameter("category", category);
        }

        params.addBodyParameter("page_size", page_size + "");

        params.addBodyParameter("page_index", "" + page_index);

        if (sort_by != 0) {
            params.addBodyParameter("sort_by", "" + sort_by);
        }

        x.http().post(params, new Callback.CommonCallback<CourseData>() {
            @Override
            public void onSuccess(CourseData result) {
                if ("list_try".equals(uri) && result.tryCourse != null) {
                    tryCourse = result.tryCourse;
                    Log.d("zzz", "no");
                    one = true;
                } else if ("list_course".equals(uri) && result.qualityCourse != null) {
                    Log.d("zzz", "no2");
                    qualityCourse = result.qualityCourse;
                    two = true;
                } else if ("list_topic".equals(uri) && result.topicCourse != null) {
                    Log.d("zzz", "yes");
                    three = true;
                    topicCourse = result.topicCourse;
                }
                if (one == true && two == true && three == true) {
                    CourseData result1 = result;
                    result1.tryCourse = tryCourse;
                    result1.qualityCourse = qualityCourse;
                    result1.topicCourse = topicCourse;
                    HomeAdapter homeAdapter = new HomeAdapter(getActivity(), result);
                    home_recyclerView.setAdapter(homeAdapter);
                    // 创建RecyclerView的数据适配器
                    // 创建适配器包装
                    WrapAdapter mWrapAdapter = new WrapAdapter<>(homeAdapter);
                    // 设置头部和尾部占据一行
                    mWrapAdapter.adjustSpanSize(home_recyclerView);
                    // 设置RecyclerView的数据适配器(适配器包装)
                    home_recyclerView.setAdapter(mWrapAdapter);
                    //添加头部
                    mWrapAdapter.addHeaderView(viewPagerView);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
